<template>
  <div class="reg">
    <router-link to="/"
      ><img
        src="https://img1.baidu.com/it/u=3397123128,1457251663&fm=253&fmt=auto&app=138&f=PNG?w=572&h=500"
        class="logo"
        alt=""
    /></router-link>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="repassword"
        label="确认密码"
        placeholder="请再次输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'Login' }">已有账号，我要登录</router-link>
  </div>
</template>

<script>
import { Notify } from "vant";
import { regAPI } from "../services/auth";
import { setToken } from "../Uitils/auth";
export default {
  data() {
    return {
      userName: "",
      password: "",
      repassword: "",
    };
  },
  methods: {
    async onSubmit() {
      if (this.password != this.repassword) {
        Notify({
          message: "两次输入的密码不一致",
          type: "warning",
        });
        return;
      }
      // setToken("123");
      const info = await regAPI({
        userName: this.userName,
        password: this.password,
      });
      console.log(info);
      if (info.code == 1) {
        Notify({
          message: "注册成功",
          type: "success",
        });
        setToken(info.data);
        // 跳转到个人中心
        this.$router.replace({
          name: "User",
        });
      } else {
        Notify({
          message: "注册异常",
          type: "danger",
        });
      }
    },
  },
};
</script>

<style scoped>
.logo {
  width: 45vw;
  display: block;
  margin: 20px auto;
  border-radius: 16px;
}
</style>
